import akshare as ak
import pandas as pd

# 场景：使用Akshare获取股票数据并开发简单的均线策略

# 获取股票日线行情数据
stock_data = ak.stock_zh_a_daily(symbol="sz000001", start_date="2022-01-01", end_date="2022-12-31")

# 计算5日和20日均线
stock_data["ma5"] = stock_data["close"].rolling(window=5).mean()
stock_data["ma20"] = stock_data["close"].rolling(window=20).mean()

# 生成买入信号（ma5向上穿越ma20）
stock_data["buy_signal"] = (stock_data["ma5"] > stock_data["ma20"]) & (stock_data["ma5"].shift(1) <= stock_data["ma20"].shift(1))

# 生成卖出信号（ma5向下穿越ma20）
stock_data["sell_signal"] = (stock_data["ma5"] < stock_data["ma20"]) & (stock_data["ma5"].shift(1) >= stock_data["ma20"].shift(1))

# 打印交易信号
signals = stock_data[["date", "buy_signal", "sell_signal"]]
print(signals)